<?php
include_once ("connect.php");
$action = $_GET['action'];
switch ($action) {
	case 'list' : //列表		
		$email =  $_GET['email'];		
		$idc_name = $_GET['idc_name'];
		$sex =  $_GET['sex'];		
		$country =  $_GET['country'];

        $where = '';
		
		echo $email." ".$idc_name." ".$sex." ".$country; 
		//$title33 = unescape($_GET['title']);
		//echo $title33;
		//exit();		
		
		$title = $_GET['title'];		
        if(!empty($title)){
			//$title22 = uniDecode($title,'gbk');
			$title22 = unescape($title);
            $where .= " and title like '%".$title22."%'";
		}	
			
        $sn = $_GET['sn'];
        if(!empty($sn)){
			//$sn22 = uniDecode($sn,'utf-8');
			$sn22 = unescape($sn);
            $where .= " and sn='$sn22'";
		}	      
		
		//echo "===>".$title." ===>".$sn;
		//echo $where;
		//exit();
				
		$result = mysql_query("SELECT COUNT(*) AS count FROM products where deleted=0".$where);
		$row = mysql_fetch_array($result, MYSQL_ASSOC);
		$count = $row['count'];
		//echo $count;		
		
		if ($count > 0) {
			$total_pages = ceil($count / $limit);
		} else {
			$total_pages = 0;
		}
		if ($page > $total_pages)
			$page = $total_pages;
		$start = $limit * $page - $limit;
		if ($start<0) $start = 0;
		$SQL = "SELECT * FROM products WHERE deleted=0".$where." ORDER BY $sidx $sord LIMIT $start , $limit";
		$result = mysql_query($SQL) or die("Couldn t execute query." . mysql_error());

		$responce->page = $page;
		$responce->total = $total_pages;
		$responce->records = $count;
		$i = 0;
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
			$responce->rows[$i]['id'] = $row[id];
			$opt = "<a href='#'>修改</a>";
			$responce->rows[$i]['cell'] = array (
				$row['sn'],
				$row['title'],
				$row['size'],
				$row['os'],
				$row['charge'],
				$row['price'],
				$opt
			);
			$i++;
		}
		//print_r($responce);
		echo json_encode($responce);
		
		break;
	case 'edit' : //修改
		$id = htmlspecialchars(stripslashes(trim($_POST['id'])));
		$email = htmlspecialchars(stripslashes(trim($_POST['email'])));
		$idc_name = htmlspecialchars(stripslashes(trim($_POST['idc_name'])));
		$sex = htmlspecialchars(stripslashes(trim($_POST['sex'])));
		$country = htmlspecialchars(stripslashes(trim($_POST['country'])));		
		$city = htmlspecialchars(stripslashes(trim($_POST['city'])));
		$address = htmlspecialchars(stripslashes(trim($_POST['address'])));
		

		//echo $email." | ".$idc_name." | ".$sex." | ".$country; 
		
		//if (mb_strlen($pro_title) < 1){
		//	die("产品名称不能为空");
		//}	
			
		$updatetime = date('Y-m-d H:i:s');
		
		$sql2 = "UPDATE order_user SET idc_name='$idc_name',sex=$sex,country='$country',city='$city',address='$address',update_time=now() WHERE id=$id";
		
		//echo "<br>".$sql2."<br>";
		//alert($sql2);
		
		$query = mysql_query($sql2);
		
		//exit(); //test!!
		
		if (mysql_affected_rows($link) != 1) {
			die("操作失败");
		} else {
			echo '1';
		}

		break;
		
		
	case 'del' : //删除
		$ids = $_POST['ids'];
		delAllSelect($ids, $link);
		break;
	case '' :
		echo 'Bad request.';
		break;
}

//批量删除操作
function delAllSelect($ids, $link) {
	if (empty ($ids))
		die("0");
	mysql_query("update products set deleted=1 where id in($ids)");
	if (mysql_affected_rows($link)) {
		echo $ids;
	} else {
		die("0");
	}
}

/*
//处理接收jqGrid提交查询的中文字符串
function uniDecode($str, $charcode) {
	$text = preg_replace_callback("/%u[0-9A-Za-z]{4}/", toUtf8, $str);
	return mb_convert_encoding($text, $charcode, 'utf-8');
}
function toUtf8($ar) {
	foreach ($ar as $val) {
		$val = intval(substr($val, 2), 16);
		if ($val < 0x7F) { // 0000-007F
			$c .= chr($val);
		}
		elseif ($val < 0x800) { // 0080-0800
			$c .= chr(0xC0 | ($val / 64));
			$c .= chr(0x80 | ($val % 64));
		} else { // 0800-FFFF
			$c .= chr(0xE0 | (($val / 64) / 64));
			$c .= chr(0x80 | (($val / 64) % 64));
			$c .= chr(0x80 | ($val % 64));
		}
	}
	return $c;
}
*/

//------------------
function unescape($str) 
{ 
    $ret = ''; 
    $len = strlen($str); 
    for ($i = 0; $i < $len; $i ++) 
    { 
        if ($str[$i] == '%' && $str[$i + 1] == 'u') 
        { 
            $val = hexdec(substr($str, $i + 2, 4)); 
            if ($val < 0x7f) 
                $ret .= chr($val); 
            else  
                if ($val < 0x800) 
                    $ret .= chr(0xc0 | ($val >> 6)) . 
                     chr(0x80 | ($val & 0x3f)); 
                else 
                    $ret .= chr(0xe0 | ($val >> 12)) . 
                     chr(0x80 | (($val >> 6) & 0x3f)) . 
                     chr(0x80 | ($val & 0x3f)); 
            $i += 5; 
        } else  
            if ($str[$i] == '%') 
            { 
                $ret .= urldecode(substr($str, $i, 3)); 
                $i += 2; 
            } else 
                $ret .= $str[$i]; 
    } 
    return $ret; 
}



?>